<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    let arr1 = [
        {totlehour:"50", state:1,project:"a"},
        {totlehour:"40",state:2,project:"a"},
        {totlehour:"30",state: 3,project:"a"},
        {totlehour:"20",state: 1,project:"b"},
        {totlehour:"10",state: 1,project:"c"}

    ]
    let arr2 = [
        {
            totlehour1:'10',
            totlehour2:'20',
            totlehour3:'30'
        }
    ]
    // arr1.forEach((item,index)=>{
    //     arr2[0]['totlehour'+(index+1)] = item.totlehour
    // })
    function convert (arr1) {
        var map1 = {};
        while(arr1.length) {
            let current = arr1.pop(); // 会影响原数组
            map1[current.project] = map1[current.project] || [];
            map1[current.project].push(current);
        }
        return Object.keys(map1).map(key => map1[key]);
    }
    var arrayTwo = convert(arr1)
    console.log(arrayTwo)
    // console.log(arrayTwo[0][0].totlehour)

    var s1 = []
    var s2 = []
    for (let i=0; i<arrayTwo.length; i++) {
        // s1.push({
        //     totlehour1: arrayTwo[i][j].totlehour,
        //     totlehour2:arrayTwo[i][j].totlehour,
        //     totlehour3:arrayTwo[i][j].totlehour
        // })
        s1.push(arrayTwo[i])
        // console.log(arrayTwo[i])
        for (let j=0; j < arrayTwo[i].length; j++) {
            // arr2[i]['totlehour'] =arrayTwo[i][j].totlehour
            // console.log(arr2[i]['totlehour'+(i+1)])
            let project = arrayTwo[i][j].project
            console.log(project)
            switch (state) {
                case "a":
                    console.log(arrayTwo[i][j].totlehour)
                    break;
                case "":
                    console.log(arrayTwo[i][j].totlehour)

                    break;
                case 3:
                    console.log(arrayTwo[i][j].totlehour)

                    break;
            }

        }
    }
</script>
</body>
</html>